<template>
	<view class="list_menu">
		<form v-model="fromData">
			<view class="label_class title_inpit">
				<input type="text" v-model="fromData.title" placeholder="请输入代办名称"
					style="height: 65rpx; border-bottom: 1px solid rgb(229, 229, 229); font-size: 14px; text-indent: 8px;" />
			</view>
			<view class="label_class">
				<fui-label>优先级</fui-label>
				<view>
					<uni-data-select v-model="fromData.priority_id" :localdata="priority"
						@change="changepriority"></uni-data-select>
				</view>
			</view>

			<view class="label_class">
				<fui-label>选择开始日期和结束日期</fui-label>
				<view class="example-body">
					<uni-datetime-picker v-model="datetimerange" type="daterange" />
				</view>
			</view>

			<view class="label_class">
				<fui-label>待办类别</fui-label>
				<view>
					<uni-data-select v-model="fromData.to_do_class" :localdata="todoclass"
						@change="changeToDoClass"></uni-data-select>
				</view>

			</view>


			<view class="label_class">
				<fui-label>通知方式</fui-label>
				<view>
					<uni-data-select v-model="fromData.inform" :localdata="inform" @change="changeInform"></uni-data-select>
				</view>
			</view>


			<view class="label_class">
				<fui-label>待办内容</fui-label>
				<view>
					<fui-textarea placeholder="请输入内容" v-model="fromData.content"
						style="border: 1px solid rgb(229, 229, 229); border-radius: 10rpx;"></fui-textarea>
				</view>
			</view>
			<view class="label_class">
				<fui-label>定时提醒</fui-label>

				<view>
					<uni-data-select v-model="fromData.remind" :localdata="remind"></uni-data-select>
				</view>
			</view>
		</form>
		<fui-button background="#fff" color="#465CFF" borderColor="#465CFF" @click="add_todo" radius="0"
			style="margin-top: 30rpx;" v-show="add_is">待办添加</fui-button>
		<fui-button background="#fff" color="#465CFF" borderColor="#465CFF" @click="update" radius="0"
			style="margin-top: 30rpx;" v-show="update_is">待办更新</fui-button>

		<view class="label_class">
			<fui-dialog :show="show" maskClosable @click="onClick" @close="onClose">
				<fui-input v-model="fromData.convey" :bottomLeft="0" placeholder="请输入内容"></fui-input>
			</fui-dialog>
		</view>
	</view>
</template>

<script>
	import {
		httpRequest
	} from '../../../utils/request.js'
	export default {
		data() {

			return {
				add_is: true,
				update_is: false, // 是否是更新
				info_btn: "添加待办",
				route_id: 0,
				show: false,
				fromData: {},
				value: 0,
				priority: [{
					value: 3,
					text: "常规"
				}, {
					value: 2,
					text: "重要"
				}, {
					value: 1,
					text: "紧急"
				}],
				inform: [{
						value: 0,
						text: "邮箱"
					},
					{
						value: 1,
						text: "短信"
					},
				],
				remind: [{
						value: 0,
						text: "每天"
					},
					{
						value: 1,
						text: "每周"
					},
					{
						value: 2,
						text: "每半个月"
					},
					{
						value: 3,
						text: "每个月"
					},
				],
				todoclass: [], // 待办类别
				datetimerange: '', // 时间
			}
		},
		watch: {},
		methods: {
			// 优先级
			changepriority(e) {

				this.fromData.priority = e
			},
			// 通知方式
			changeInform(e) {
				this.fromData.inform = e
				this.show = true
				if (e == 0) {
					for (var i = 0; i < this.$store.state.systemparameter.length; i++) {
						this.fromData.convey = this.$store.state.systemparameter[i].email.email
					}
				} else {
					this.fromData.convey = ""
				}
			},
			// 待办类别选择
			changeToDoClass(e) {

				this.fromData.to_do_class = e
			},
			add_todo() {
				// 时间处理
				for (var i = 0; i < this.datetimerange.length; i++) {
					switch (i) {
						case 0:
							this.fromData.start_time = this.datetimerange[i]
							break
						case 1:
							this.fromData.over_time = this.datetimerange[i]
							break
						default:
							break
					}
				}
				let query = {
					title: this.fromData.title,
					desc: this.fromData.content,
					priority: this.fromData.priority,
					inform: this.fromData.inform,
					convey: this.fromData.convey,
					start_time: this.fromData.start_time,
					to_do_class: this.fromData.to_do_class,
					over_time: this.fromData.over_time,
					remind:this.fromData.remind
				}
				httpRequest("/api/add_todo", "post", query).then((res) => {

					uni.showToast({
						title: '待办成功',
						icon: "success"
					});
					this.datetimerange = ""
					this.fromData = {}
				}).catch(err => {

					uni.showToast({
						icon: 'error',
						title: err,
						position: 'center',
						duration: 1500
					})
				})
			},
			onClose() {
				this.show = false
			},
			onClick() {
				this.show = false
			},
			// 待办类别请求
			GetAllToDoClass() {
				httpRequest("/api/todo_class", "get", "").then((res) => {
					// this.todoclass = res

					for (var i = 0; i < res.length; i++) {
						let data = {
							value: res[i].Id,
							text: res[i].title
						}

						this.todoclass.push(data)
					}
				}).catch(err => {

					uni.showToast({
						icon: 'error',
						title: err,
						position: 'center',
						duration: 1500
					})
				})
			},
			/* 编辑待办 */
			update() {
				// 时间处理
				for (var i = 0; i < this.datetimerange.length; i++) {
					switch (i) {
						case 0:
							this.fromData.start_time = this.datetimerange[i]
							break
						case 1:
							this.fromData.over_time = this.datetimerange[i]
							break
						default:
							break
					}
				}
				let query = {
					id: parseInt(this.route_id),
					title: this.fromData.title,
					desc: this.fromData.content,
					priority: this.fromData.priority,
					inform: this.fromData.inform,
					convey: this.fromData.convey,
					start_time: this.fromData.start_time,
					to_do_class: this.fromData.to_do_class,
					over_time: this.fromData.over_time
				}
				httpRequest("/api/todo_update", "put", query).then((res) => {
					uni.showToast({
						title: '待办添加成功',
						icon: "success"
					});
					this.datetimerange = ""
					this.fromData = {}
				}).catch(err => {

					uni.showToast({
						icon: 'error',
						title: err,
						position: 'center',
						duration: 1500
					})
				})
			},
			/* 加载需要更新的待办信息 */
			get_todo_id() {
				let query = {
					id: this.route_id
				}
				httpRequest("/api/todo_concreteness", "get", query).then((res) => {
					let start_time = ""
					let over_time
					for (var i = 0; i < this.datetimerange.length; i++) {
						switch (i) {
							case 0:
								start_time = this.datetimerange[i]
								break
							case 1:
								over_time = this.datetimerange[i]
								break
							default:
								break
						}
					}
					this.fromData = {
						title: res.title,
						priority_id: res.PriorityID,
						// convey: email,
						to_do_class: res.ToDoClassID,
						inform: res.inform,
						content: res.desc,


					}
				})
			},
			// 加载所有系统配置信息
			get_system_all() {
				httpRequest("/api/system/get_all_system", "get").then((res) => {
					let system = []
					for (let i = 0; i < res.length; i++) {
						system = [{
							'email': {
								'id': res[i].id,
								"user_id": res[i].user_id,
								'email': res[i].email,
								'created_at': res[i].created_at,
								'update_at': res[i].updated_at
							}
						}]
					}
					this.$store.commit("SystemParameter", system)
					for (var i = 0; i < this.$store.state.systemparameter.length; i++) {
						this.fromData.convey = this.$store.state.systemparameter[i].email.email
					}
				})

			}
		},
		onShow() {
			this.GetAllToDoClass()
			this.get_system_all()


		},
		onLoad(option) {

			this.route_id = option.id
			if (this.route_id == undefined) {
				this.add_is = true
				this.update_is = false
			} else {
				this.add_is = false
				this.update_is = true
				this.get_todo_id() // 只有是更新待办的时候才发送具体待办详情请求
			}
		}


	}
</script>

<style scoped>
	.list_menu {
		width: 96%;
		margin: 0 auto;
	}

	.label_class {
		margin-bottom: 20rpx;
	}

	.label_class>view {
		margin-top: 12rpx;
	}

	.title_inpit {
		margin-top: 15rpx;
	}
</style>